$(document).ready(function(){
    $("#GroupDescription").width( $("#group_description").width() - 1 );
    $("#save_description, #add_designee, #add_member, #add_guest").button();
    bindDeleteButtons();
    bindAddMemberElement();
    bindSaveDescription();
    bindAddDesigneeButton();
    bindDeleteDesigneeButton();
    bindAddGuestElement();
});


function bindAddGuestElement(){
    
    $("#add_guest").bind('click', function(){
        $("#groupattrs").append('<div id="confirm_dialog_add_guest" style="width: auto"><p id="add_guest_text"></p></div>');
        $('#add_guest_text').append(
            '<input type="text" id="user_search" value="" /><input type="hidden" id="member_dn" value="" /> ' +
            '<img src="../images/progress.gif" id="UserProgress" style="display:none;" align="absmiddle"/>'
        );
        
        $("#user_search").autocomplete({
            source: function(request, response){
                $("#UserProgress").show();
                $.ajax({
                    type:'GET',url: $("#AjaxController").val() + "?api=UserSearch&userSearchString=" + request.term,dataType: "json",
                    success: function( json ) {
                        $("#UserProgress").hide();
                        response( $.map( json.DATA, function( n ) {//this is a auto returned variable.  jquery kungfu
                            return {value: n[6], id: n[0], url: n[1] }
                        }));
                    }
                });
            },
            minLength: 3,
            delay: 0,
            select: function(event, ui){
                $("#member_dn").val(ui.item.id);
            }
        });
        
        var confirm_dialog_add_guest = $('#confirm_dialog_add_guest').dialog({
        autoOpen: false, modal: true,
        title: '<span class="ui-icon ui-icon-alert" style="float:left;"></span> Add User as guest?',
        resizable: false,
        open: function(event, ui){$("div.ui-dialog-titlebar").width($(".ui-dialog").width());},
        close: function(event, ui){$(this).dialog("destroy")},
        buttons: {
            "Add User": function(){
                $(this).dialog("close");
                var PostData = {UserDN: $("#member_dn").val(),GroupDN: $("#GroupDN").val(),GuestType: 1};               
                $.ajax({ type: "POST", url: $("#AjaxController").val() + "?api=AddGuestToGroup", async: false, data: PostData,
                    success: function(){return false;},
                    complete: function(){  },//location.reload()
                    error: function(){return false;}
                }); 
            },
            Cancel: function(){
                $(this).dialog("close");
                return false;
            }
        }
        });
        confirm_dialog_add_guest.dialog('open'); 
    });    
}



function bindDeleteDesigneeButton(){
    
    $(".delete_designee").bind('click', function(){
        var PostData = {DesigneeID: this.id, GroupDN: $("#GroupDN").val()};
        var UserAccount = $(this).siblings()[0];
        $("#main").append('<div id="confirm_dialog_delete_designee" style="width: auto;"><p id="confirm_text"></p></div>');
        $('#confirm_text').append($(UserAccount).attr('id'));
        
        var $confirm_dialog_delete_designee = $('#confirm_dialog_delete_designee').dialog({
            autoOpen: false, modal: true,
            title: '<span class="ui-icon ui-icon-alert" style="float:left;"></span> Remove Designee?',
            resizable: false,
            open: function(event, ui){$("div.ui-dialog-titlebar").width($(".ui-dialog").width());},
            close: function(event, ui){$(this).dialog("destroy")},
            buttons: {
                "Remove User": function(){
                    $(this).dialog("close");                    
                    $.ajax({ type: "POST", url: $("#AjaxController").val() + "?api=RemoveDesignee", async: false, data: PostData,
                        success: function(){return false;},
                        complete: function(jqXHR){ location.reload() },// },//
                        error: function(){return false;}
                    });
                },
                Cancel: function(){
                    $(this).dialog("close");
                    return false;
                }
            }
        });
        $confirm_dialog_delete_designee.dialog('open');             
    });    
}


function bindAddDesigneeButton(){
    $("#add_designee").bind('click', function(){
        
        $("#groupattrs").append('<div id="confirm_dialog_add_designee" style="width: auto"><p id="addmember_text"></p></div>');
        
        $('#addmember_text').append(
            '<input type="text" id="user_search" value="" /><input type="hidden" id="designee_dn" value="" /> ' +
            '<img src="../images/progress.gif" id="UserProgress" style="display:none;" align="absmiddle"/>'
        );
        
        $("#user_search").autocomplete({
            source: function(request, response){
                $("#UserProgress").show();
                $.ajax({
                    type:'GET',url: $("#AjaxController").val() + "?api=UserSearch&userSearchString=" + request.term,
                    dataType: "json",
                    success: function( json ) {
                        $("#UserProgress").hide();
                        response( $.map( json.DATA, function( n ) {//this is a auto returned variable.  jquery kungfu
                            return {value: n[6], id: n[0], url: n[1] }
                        }));
                    }
                });
            },
            minLength: 3,delay: 0,
            select: function(event, ui){
                $("#designee_dn").val(ui.item.id);                
            }
        });
        
        var $confirm_dialog_add_designee = $('#confirm_dialog_add_designee').dialog({
            autoOpen: false,modal: true,
            title: '<span class="ui-icon ui-icon-alert" style="float:left;"></span> Add User as Designee?',
            resizable: false,
            open: function(event, ui){$("div.ui-dialog-titlebar").width($(".ui-dialog").width());},
            close: function(event, ui){$(this).dialog("destroy")},
            buttons: {
                "Add User": function(){
                    $(this).dialog("close");
                    var PostData = {UserDN: $("#designee_dn").val(),GroupDN: $("#GroupDN").val()};               
                    $.ajax({ type: "POST", url: $("#AjaxController").val() + "?api=AddDesignee", async: false, data: PostData,
                        success: function(){return false;},
                        complete: function(){ location.reload()  },//
                        error: function(){return false;}
                    }); 
                },
                Cancel: function(){
                    $(this).dialog("close");
                    return false;
                }
            }
        });
        $confirm_dialog_add_designee.dialog('open'); 
    });
}




function bindSaveDescription(){
    $("#save_description").bind('click', function(){
        var PostData = {GroupDN: $("#GroupDN").val(),GroupDescription: $('#GroupDescription').val()};
        $.ajax({ type: "POST", url: $("#AjaxController").val() + "?api=UpdateGroupDescription", async: false, data: PostData,
            success: function(){return false;},
            complete: function(jqXHR){ location.reload() },//
            error: function(){return false;}
        });        
    });
}




function bindDeleteButtons(){
    
    $(".delete").bind('click', function(){
        var Parent = $(this).parent();
        var UserAccount = $(this).attr('id');
        var RosettaID = $(Parent).attr('id');
        var PostData = {GroupDN: $("#GroupDN").val(),ViewUser: $("#ViewUser").val(),ViewGroup: $("#ViewGroup").val(),RosettaID: RosettaID,UserAccount: UserAccount};
        
        $("#groupattrs").append('<div id="confirm_dialog_delete_member" style="width: auto;"><p id="confirm_text"></p></div>');
        $('#confirm_text').append(UserAccount);
        
        var $confirm_dialog_delete_member = $('#confirm_dialog_delete_member').dialog({
            autoOpen: false, modal: true,
            title: '<span class="ui-icon ui-icon-alert" style="float:left;"></span> Remove User?',
            resizable: false,
            open: function(event, ui){$("div.ui-dialog-titlebar").width($(".ui-dialog").width());},
            close: function(event, ui){$(this).dialog("destroy")},
            buttons: {
                "Remove User": function(){
                    $(this).dialog("close");                    
                    $.ajax({ type: "POST", url: $("#AjaxController").val() + "?api=RemoveUserFromGroup", async: false, data: PostData,
                        success: function(){return false;},
                        complete: function(jqXHR){ location.reload() },//
                        error: function(){return false;}
                    });
                },
                Cancel: function(){
                    $(this).dialog("close");
                    return false;
                }
            }
        });
        $confirm_dialog_delete_member.dialog('open');             
    });    
}




function bindAddMemberElement(){
    
    $("#add_member").bind('click', function(){
        $("#groupattrs").append('<div id="confirm_dialog_add_member" style="width: auto"><p id="addmember_text"></p></div>');
        $('#addmember_text').append(
            '<input type="text" id="user_search" value="" /><input type="hidden" id="member_dn" value="" /> ' +
            '<img src="../images/progress.gif" id="UserProgress" style="display:none;" align="absmiddle"/>'
        );
        
        $("#user_search").autocomplete({
            source: function(request, response){
                $("#UserProgress").show();
                $.ajax({
                    type:'GET',url: $("#AjaxController").val() + "?api=UserSearch&userSearchString=" + request.term,dataType: "json",
                    success: function( json ) {
                        $("#UserProgress").hide();
                        response( $.map( json.DATA, function( n ) {//this is a auto returned variable.  jquery kungfu
                            return {value: n[6], id: n[0], url: n[1] }
                        }));
                    }
                });
            },
            minLength: 3,
            delay: 0,
            select: function(event, ui){
                $("#member_dn").val(ui.item.id);
            }
        });
        
        var $confirm_dialog_add_member = $('#confirm_dialog_add_member').dialog({
        autoOpen: false, modal: true,
        title: '<span class="ui-icon ui-icon-alert" style="float:left;"></span> Add User?',
        resizable: false,
        open: function(event, ui){$("div.ui-dialog-titlebar").width($(".ui-dialog").width());},
        close: function(event, ui){$(this).dialog("destroy")},
        buttons: {
            "Add User": function(){
                $(this).dialog("close");
                var PostData = {UserDN: $("#member_dn").val(),GroupDN: $("#GroupDN").val()};               
                $.ajax({ type: "POST", url: $("#AjaxController").val() + "?api=AddUserToGroup", async: false, data: PostData,
                    success: function(){return false;},
                    complete: function(){ location.reload() },
                    error: function(){return false;}
                }); 
            },
            Cancel: function(){
                $(this).dialog("close");
                return false;
            }
        }
        });
        $confirm_dialog_add_member.dialog('open'); 
    });    
}

